package com.zzrc.grape.service.impl;

import com.google.gson.Gson;
import com.zzrc.apricot.dao.mapper.LogSmsSendMapper;
import com.zzrc.apricot.model.db.LogSmsSend;
import com.zzrc.grape.service.SmsService;
import com.zzrc.grape.util.BasePageData;
import com.zzrc.grape.util.WebResponseCode;
import com.zzrc.peony.dao.mapper.RecordLoanMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SmsServiceImpl implements SmsService {
    @Resource
    LogSmsSendMapper logSmsSendMapper;

    @Override
    public BasePageData getSmsInfo() {
        BasePageData basePageData = new BasePageData();
        List<LogSmsSend> logSmsSends = logSmsSendMapper.selectLimitTen();
        List<LogSmsSend> subList;
        if (logSmsSends.size() > 10) {
            subList = logSmsSends.subList(-10, -1);
        } else {
            subList = logSmsSends;
        }
        List<LogSmsSend> tmpList = new ArrayList<>();
        for (LogSmsSend logSmsSend: subList) {
            String msgBody = logSmsSend.getMsgBody();
            Gson gson = new Gson();
            Map<String, Object> map = new HashMap<>();
            map = gson.fromJson(msgBody, map.getClass());
            String newMsgBody = (String) map.get("tpl_value");
            if (newMsgBody.length() > 7) {
                newMsgBody = newMsgBody.substring(7);
            }
            logSmsSend.setMsgBody(newMsgBody);
            tmpList.add(logSmsSend);
        }
        basePageData.setCode(WebResponseCode.SUCCESS);
        basePageData.setMsg(WebResponseCode.SUCCESSINFO);
        basePageData.setData(tmpList);
        return basePageData;
    }
}
